package es.ats.codington.festival.services;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import es.ats.codington.festival.daos.PlaceDAOImpl;
import es.ats.codington.festival.domain.Building;
import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAttraction;
import es.ats.codington.festival.domain.TraditionalMarket;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.domain.Place.Real;
import es.ats.codington.festival.domain.Place.Zone;
import es.ats.codington.festival.interfaces.services.IPlaceService;

public class PlaceServiceImpl implements IPlaceService{
		
	/**
	 * Show place all.
	 *
	 * @return true, if successful
	 * @see es.ats.codington.festival.interfaces.services.IPlaceService#ShowPlaceAll()
	 */
	public boolean ShowPlaceAll(){
		Connection con = null;
		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();			
			rs = placeDAO.showAllPlace(con);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		/**if (rs) System.out.println("correct show!!");*/
		return rs;
	}
	
	/**
	 * Obtain all places.
	 *
	 * @return the array list
	 */
	public ArrayList<Integer> obtainAllPlaces() {
		Connection con = null;
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		ArrayList<Integer> pla = new ArrayList<Integer>();
		try{
			con = serviceManager.OpenConnectionToDB();			
			pla = placeDAO.obtainAllPlaces(con);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return pla;
	}
	
	public HashMap<Integer, String> obtainNameAndId(){
		HashMap<Integer, String> myPlaces = new HashMap<Integer, String>();
		
		Connection con = null;
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();

		try{
			con = serviceManager.OpenConnectionToDB();			
			myPlaces = placeDAO.obtainPlacesNameId(con);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return myPlaces;
	}
	
	
	/**
	 * Show places id.
	 *
	 * @param id the id
	 * @return true, if successful
	 */
	public boolean ShowPlacesId(int id){
		Connection con = null;
		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();			
			rs = placeDAO.showPlacesId(con, id);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		if (rs) System.out.println("correct show!!");
		return rs;
	}
	
	
	//Inserts
	
	/**
	 * Register place.
	 *
	 * @param place the place
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	public boolean registerPlace (Place place) throws Exception{
		Connection con = null;
		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();			
			rs = placeDAO.insertPlaceDAO(con, place);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		if (rs) System.out.println(" Place added!");
		return rs;
	}
	
	/**
	 * Register building.
	 *
	 * @param building the building
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	public boolean registerBuilding (Building building) throws Exception{
		Connection con = null;
		boolean rs = false;
		
		ServiceManager serviceManager = new ServiceManager();
		PlaceDAOImpl placeDAO = new PlaceDAOImpl();
		try{
			con = serviceManager.OpenConnectionToDB();			
			rs = placeDAO.insertPlaceDAO(con, building);
			serviceManager.CloseConnectionToDB(con);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		//Printing how many rows got added
		if (rs) System.out.println(" Building added!");
		return rs;
	}
	
	//Deletes
	
		/**
	 * Delete place.
	 *
	 * @param place the place
	 * @return true, if successful
	 * @throws Exception the exception
	 */
	public boolean deletePlace (Place place) throws Exception{
			Connection con = null;
			boolean rs = false;
			
			ServiceManager serviceManager = new ServiceManager();
			PlaceDAOImpl placeDAO = new PlaceDAOImpl();
			try{
				con = serviceManager.OpenConnectionToDB();			
				rs = placeDAO.deletePlaceDAO(con, place);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			//Printing how many rows got added
			if (rs) System.out.println("Place delete!");
			return rs;
		}
		
		/**
		 * Delete building.
		 *
		 * @param building the building
		 * @return true, if successful
		 * @throws Exception the exception
		 */
		public boolean deleteBuilding (Building building) throws Exception{
			Connection con = null;
			boolean rs = false;
			
			ServiceManager serviceManager = new ServiceManager();
			PlaceDAOImpl placeDAO = new PlaceDAOImpl();
			try{
				con = serviceManager.OpenConnectionToDB();			
				rs = placeDAO.deletePlaceDAO(con, building);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			//Printing how many rows got added
			if (rs) System.out.println(" Building delete!");
			return rs;
		}
		
		//Updates
		
		/**
		 * Update place.
		 *
		 * @param place the place
		 * @return true, if successful
		 * @throws Exception the exception
		 */
		public boolean updatePlace (Place place) throws Exception{
			Connection con = null;
			boolean rs = false;
			
			ServiceManager serviceManager = new ServiceManager();
			PlaceDAOImpl placeDAO = new PlaceDAOImpl();
			try{
				con = serviceManager.OpenConnectionToDB();			
				rs = placeDAO.updatePlaceDAO(con, place);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			//Printing how many rows got added
			if (rs) System.out.println("Place update!");
			return rs;
		}
		
		/**
		 * Update building.
		 *
		 * @param building the building
		 * @return true, if successful
		 * @throws Exception the exception
		 */
		public boolean updateBuilding (Building building) throws Exception{
			Connection con = null;
			boolean rs = false;
			
			ServiceManager serviceManager = new ServiceManager();
			PlaceDAOImpl placeDAO = new PlaceDAOImpl();
			try{
				con = serviceManager.OpenConnectionToDB();			
				rs = placeDAO.updatePlaceDAO(con, building);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			//Printing how many rows got added
			if (rs) System.out.println("Building update!");
			return rs;
		}
}

